<?php
/**
 * Created by PhpStorm.
 * User: zhangbing
 * Date: 2017/3/16 0016
 * Time: 16:06
 */
namespace app\admin\controller;
use app\models\Models;

class ActivityType extends Base{
    /**
     * 活动类型列表
     * @return \think\response\View
     */
    public function typeList(){
        $type = new Models('activityType');
        $list = $type->_select();
        return view('typeList',['list'=>$list]);
    }

    /**
     * 活动类型添加
     * @return array|\think\response\View
     */
    public function typeAdd(){
        if(request()->isAjax()){
            $post = input('param.');
            $type = new Models('activityType');
            try{
                // 添加场景验证
                $validate = $this->validate($post,'orderType.add');
                if(true !== $validate){
                    throw new \LogicException($validate,1000);
                }
                $bool = $type->_add($post);
                if(!$bool){
                    throw new \LogicException('操作失败',1010);
                }
            } catch (\Exception $e){
                return self::formatResult($e->getCode(),$e->getMessage());
            }
            return self::formatSuccessResult();
        }
        return view('typeAdd');
    }

    /**
     * 活动类型修改
     * @return array|\think\response\View
     */
    public function typeSave(){
        $type = new Models('activityType');
        if(request()->isAjax()){
            $post = input('param.');
            try{
                // 修改场景验证
                $validate = $this->validate($post,'orderType.save');
                if(true !== $validate){
                    throw new \LogicException($validate,1000);
                }
                $bool = $type->_save($post);
                if(false === $bool){
                    throw new \LogicException('操作失败',1010);
                }
            } catch (\Exception $e){
                return self::formatResult($e->getCode(),$e->getMessage());
            }
            return self::formatSuccessResult();
        }
        $get = input('param.');
        $info = $type->_find($get['id']);
        return view('typeSave',['info'=>$info]);
    }

    /**
     * 启用
     * @return array
     */
    public function start(){
        if(request()->isAjax()){
            $post = input('param.');
            $type = new Models('activityType');
            try{
                $bool = $type->setValue(['id'=>$post['id']],['status'=>$type::STATUS_ON]);
                if(!$bool){
                    throw new \LogicException('操作失败',1010);
                }
            } catch (\Exception $e){
                return self::formatResult($e->getCode(),$e->getMessage());
            }
            return self::formatSuccessResult();
        }
    }

    /**
     * 停用
     * @return array
     */
    public function stop(){
        if(request()->isAjax()){
            $post = input('param.');
            $type = new Models('activityType');
            try{
                $bool = $type->setValue(['id'=>$post['id']],['status'=>$type::STATUS_OFF]);
                if(!$bool){
                    throw new \LogicException('操作失败',1010);
                }
            } catch (\Exception $e){
                return self::formatResult($e->getCode(),$e->getMessage());
            }
            return self::formatSuccessResult();
        }
    }
}